//
//  CFDProblem.cpp
//  NSESolver
//
//  Created by Patrick Zulian on 3/29/11.
//  Copyright 2011 ICS - USI - Lugano. All rights reserved.
//


#include "CFDProblem.h"

namespace nsesolver{
    
    int CFDProblem::computeMaxLoc(const CartesianTopology & cart, const int amax, const int dim)
    {
        return floor(amax / cart.getDimensions()[dim]) + ( cart.getCoordinates()[dim] < ( amax % cart.getDimensions()[dim] ));
    }
    
    
    int CFDProblem::computeGlobalIndex(const CartesianTopology & cart, const int amax, const int dim)
    {
        
        int offset=cart.getCoordinates()[dim]-cart.getDimensions()[dim]-1;
        offset=std::max(0, offset);
        
        return floor( amax / cart.getDimensions()[dim] ) * cart.getCoordinates()[dim] + 
        std::min( cart.getCoordinates()[dim], amax %  cart.getDimensions()[dim]) - offset; 
    }
    
    
    CFDProblem::CFDProblem(const int imaxGlobal, const int jmaxGlobal, const double alpha,
                           const Vector2d & southWestCorner, const Vector2d & northEastCorner, 
                           const BoundaryHandler &bc, const InitialValue &iv,  const CartesianTopology &cart)
    : 
    imax(computeMaxLoc(cart, imaxGlobal, 0)), jmax(computeMaxLoc(cart, jmaxGlobal, 1)), 
    iGlobal(computeGlobalIndex(cart, imaxGlobal, 0 )), jGlobal(computeGlobalIndex(cart, jmaxGlobal, 1 )),
    
    alpha(alpha),
    dx( (northEastCorner[0] - southWestCorner[0])/(imaxGlobal + 1.0) ), 
    dy( (northEastCorner[1] - southWestCorner[1])/(jmaxGlobal + 1.0) ),
    
    southWestCorner( southWestCorner ), northEastCorner( northEastCorner ),
    bc(bc), iv(iv),
    
    u(imax + 1, jmax + 2), v(imax+2, jmax+1), p(imax+2, jmax+2),
    cart(cart)
    { 
        setInitialValues();
        p.allSet(0);
    }
    
    CFDProblem::~CFDProblem()
    { }
    
}
